﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ycjw.PipeLine.WPF
{
    /****************************************************************************************/
    /*
    功    能:放置三维场景的DocWindow
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    /// <summary>
    /// BForm.xaml 的交互逻辑
    /// </summary>
    public partial class BForm : Window
    {
        readonly IResposeWPF m_IResposeWPF;
        public BForm(IResposeWPF respose)
        {
            InitializeComponent();
            Init();
            m_IResposeWPF = respose;
        }
        public System.Windows.Shapes.Ellipse EllipseAll
        {
            get
            {
                return m_EllipseAll;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseWS
        {
            get
            {
                return m_EllipseDL;
            }
        }
        public System.Windows.Shapes.Ellipse EllipseYS
        {
            get
            {
                return m_EllipseDX;
            }
        }

        public System.Windows.Controls.Image ImageInfo
        {
            get
            {
                return m_ImageInfo;
            }
        }
        public System.Windows.Controls.Image ImagePan
        {
            get
            {
                return m_ImagePan;
            }
        }
        public System.Windows.Controls.Image ImageTerrain
        {
            get
            {
                return m_ImageTerrain;
            }
        }

        public void SetInfo(string infoName,NameValueCollection nvc)
        {
            if (this.Visibility == Visibility.Hidden) return;
            if (nvc.Count == 0)
            {
                m_InfoGrid.Visibility = Visibility.Hidden;
                return;
            }
            InfoScrollViewer.Height = this.m_InfoBorder.ActualHeight - 40;
            m_InfoGrid.Visibility = Visibility.Visible;
            m_InfoItems.Children.Clear();
            m_InfoTitle.Content = infoName;
            for (int i = 0;i < nvc.Count; i++)
            {
                string nvKey = nvc.GetKey(i);
                string nvValue = nvc.Get(i);
                WPF.PropertiesEx prop = new WPF.PropertiesEx(nvKey, nvValue);
                m_InfoItems.Children.Add(prop);
            }
        }
        private void SetImageSource(System.Windows.Controls.Image image,string fileName)
        {
            string path;
            if (!System.IO.File.Exists(fileName))
            {
                path = Commons.Paths.ApplicationResourcesDir + "\\" + fileName;
            }
            else
            {
                path = fileName;
            }
            Bitmap bitmap = new Bitmap(path);
            image.Tag = path;
            image.Source = BitmapToBitmapSource(bitmap);
        }
        void Init()
        {
            SetImageSource(m_ImageLogoBg, "BigScreen\\形状512.png");
            SetImageSource(m_ImageLogoICO, "BigScreen\\logo.png");
            SetImageSource(m_ImageTitle, "BigScreen\\云创经纬三维管线信息系统.png");

            SetImageSource(m_ImageChartTitle01, "BigScreen\\RightArrow.png");
            SetImageSource(m_ImageChartTitle02, "BigScreen\\RightArrow.png");
            SetImageSource(m_ImageChartTitle03, "BigScreen\\RightArrow.png");

            SetImageSource(m_ImageStaticsPanelIcon, "BigScreen\\EllipseDL-blue.png");

            SetImageSource(m_ImageExist, "exit.png");
            m_ImageExist.MouseUp += ImageExist_MouseUp;

            SetImageSource(m_ImageHome, "BigScreen\\ImageHome.png");
            m_ImageHome.MouseUp += ImageHome_MouseUp;
            m_ImageHome.MouseEnter += ImageButton_MouseEnter;
            m_ImageHome.MouseLeave += ImageButton_MouseLeave;

            SetImageSource(m_ImageInfo, "BigScreen\\ImageInfo.png");
            m_ImageInfo.MouseEnter += ImageButton_MouseEnter;
            m_ImageInfo.MouseLeave += ImageButton_MouseLeave;

            SetImageSource(m_ImagePan, "BigScreen\\ImagePan.png");
            m_ImagePan.MouseEnter += ImageButton_MouseEnter;
            m_ImagePan.MouseLeave += ImageButton_MouseLeave;

            SetImageSource(m_ImageTerrain, "BigScreen\\ImageTerrain.png");
            m_ImageTerrain.MouseEnter += ImageButton_MouseEnter;
            m_ImageTerrain.MouseLeave += ImageButton_MouseLeave;

            InfoScrollViewer.MouseEnter += InfoScrollViewer_MouseEnter;
            InfoScrollViewer.MouseLeave += InfoScrollViewer_MouseLeave;

            Grid oo = this.Content as Grid;
            UIElementCollection Childrens = oo.Children;
            foreach (UIElement ui in Childrens)
            {
                IEnumerable<Grid> gds = FindVisualChildren<Grid>(ui);
                foreach (var gd in gds)
                {
                    if (gd.Tag != null && gd.Tag.ToString() == "BTN")
                    {
                        /*
                        gd.MouseEnter += Grid_MouseEnter;
                        gd.MouseLeave += Grid_MouseLeave;
                        */
                    }
                    if (gd.Parent is StackPanel)
                    {
                        if ((gd.Parent as StackPanel).Name.StartsWith("TopContext"))
                        {
                            /*
                            gd.Opacity = .5;
                            gd.MouseEnter += Grid_MouseEnter;
                            gd.MouseLeave += Grid_MouseLeave;
                            */
                        }
                    }
                }
                IEnumerable<Ellipse> ee = FindVisualChildren<Ellipse>(ui);
                foreach (Ellipse e in ee)
                {
                    if (e.Tag != null && e.Tag.ToString() == "TYPES")
                    {
                        e.Cursor = Cursors.Hand;
                        if (e.Parent is Grid)
                        {
                            Grid g = e.Parent as Grid;
                            string path = Commons.Paths.ApplicationResourcesDir + "\\BigScreen\\" + e.Name.Replace("m_", "") + ".png";
                            g.Tag = path;
                            e.MouseEnter += MenuMouseEnter;
                            e.MouseLeave += MenuMouseLeave;
                            e.MouseUp += MenuMouseUp;
                            if (!System.IO.File.Exists(path)) continue;
                            Bitmap bitmap = new Bitmap(path);
                            g.Background = new ImageBrush(BitmapToBitmapSource(bitmap));
                        }
                    }
                }
            }
        }

        private void MenuMouseUp(object sender, MouseButtonEventArgs e)
        {
            Ellipse ell = sender as Ellipse;
            Grid g = ell.Parent as Grid;
            string defaultPath = g.Tag as string;

            string blueBg = defaultPath.Replace(".png", "-blue.png");
            SetImageSource(m_ImageStaticsPanelIcon, blueBg);
        }

        private void ImageButton_MouseLeave(object sender, MouseEventArgs e)
        {
            System.Windows.Controls.Image img = sender as System.Windows.Controls.Image;
            string defaultPath = img.Tag as string;
            Bitmap bitmap = new Bitmap(defaultPath);
            img.Source = BitmapToBitmapSource(bitmap);
        }

        private void ImageButton_MouseEnter(object sender, MouseEventArgs e)
        {
            System.Windows.Controls.Image img = sender as System.Windows.Controls.Image;
            string defaultPath = img.Tag as string;
            string bluePath = defaultPath.Replace(".png","-blue.png");
            Bitmap bitmap = new Bitmap(bluePath);
            img.Source = BitmapToBitmapSource(bitmap);
        }

        private void MenuMouseLeave(object sender, MouseEventArgs e)
        {
            Ellipse ell = sender as Ellipse;
            Grid g = ell.Parent as Grid;
            string defaultPath = g.Tag as string;

            Bitmap bitmap = new Bitmap(defaultPath);
            g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
        }

        private void MenuMouseEnter(object sender, MouseEventArgs e)
        {
            Ellipse ell = sender as Ellipse;
            Grid g = ell.Parent as Grid;
            string defaultPath = g.Tag as string;

            string blueBg = defaultPath.Replace(".png","-blue.png");
            Bitmap bitmap = new Bitmap(blueBg);
            g.Background = new System.Windows.Media.ImageBrush(BitmapToBitmapSource(bitmap));
        }

        private void Grid_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Grid gd = sender as Grid;
            gd.Opacity = .5;
        }

        private void Grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Grid gd = sender as Grid;
            gd.Opacity = 1;
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T t)
                    {
                        yield return t;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        private void InfoScrollViewer_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
        }

        private void InfoScrollViewer_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            InfoScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        }

        private void ImageHome_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.m_IResposeWPF.ResposeWPF("VIEWENTIRE");
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.Key == Key.Escape)
                this.Hide();
        }
        private void ImageExist_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Hide();
        }

        //public DockPanel StackPanel01
        //{
        //    get { return this.m_PanelLeftCahrt01; }
        //}
        public Grid StackPanel02
        {
            get { return this.m_PanelLeftCahrt02; }
        }
        public Grid StackPanel03
        {
            get { return this.m_PanelLeftCahrt03; }
        }
        public Grid StackPanel04
        {
            get { return this.m_PanelLeftCahrt04; }
        }

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        //将 Bitmap转换为BitmapSource
        //使用过System.Drawing.Bitmap后一定要用DeleteObject释放掉对象，不然内存不释放，很快系统内存就消耗光了。
        private static ImageSource BitmapToBitmapSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
            {
                throw new System.ComponentModel.Win32Exception();
            }

            return wpfBitmap;
        }
    }
}
